package com.jlnx.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;


@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		String entNo =request.getParameter("account");
		String password =request.getParameter("password");
		System.out.println(entNo + ":" + password);
		
		EnterpriseDaoImpl dao = new EnterpriseDaoImpl();
				
		//处理请求数据
		    //校验
		 Enterprise e=dao.checkEnterpriseLogin(entNo, password);
		 System.out.println(e);
		//响应处理结果
		 if(e!=null) {
			 //获取session对象
			 HttpSession hs=request.getSession();
			// 设置session的最长生存时间为30天（以秒为单位）
			 int sessionTimeout = 370 * 24 * 60 * 60; // 370天
			 hs.setMaxInactiveInterval(sessionTimeout);
			 //将用户信息存储到session中
			 hs.setAttribute("enterprise", e);
			if(e.getRole() == 1) {
			    //request.setAttribute("name", e.getName());
			    // 设置响应的内容类型为JSON
				JSONObject jsonObject = (JSONObject) JSONObject.toJSON(e);
				jsonObject.put("code", 200);
				jsonObject.put("role", 1);
			    System.out.println(jsonObject);
		        // 将响应信息写入到响应对象中
		        response.getWriter().println(jsonObject);
			}else if(e.getRole() == 2) {
			    //request.setAttribute("name", e.getName());
			      // 设置响应的内容类型为JSON
				JSONObject jsonObject = (JSONObject) JSONObject.toJSON(e);
				jsonObject.put("code", 200);
				jsonObject.put("role", 2);
			    System.out.println(jsonObject);
		     // 将响应信息写入到响应对象中
			    //response.setStatus(HttpServletResponse.SC_OK);
		        response.getWriter().println(jsonObject);
			}else if(e.getRole() == 3) {
			      // 设置响应的内容类型为JSON
				JSONObject jsonObject = (JSONObject) JSONObject.toJSON(e);
				jsonObject.put("code", 200);
				jsonObject.put("role", 3);
			    System.out.println(jsonObject);
		     // 将响应信息写入到响应对象中
			    //response.setStatus(HttpServletResponse.SC_OK);
		        response.getWriter().println(jsonObject);
			}else {
				request.setAttribute("name", e.getName());
			    //request.getRequestDispatcher("/client/index2.jsp").forward(request, response);
			}
		 }else {
			JSONObject responseObject = new JSONObject();
			responseObject.put("message", "用户名或密码错误!");
			responseObject.put("code", 500);
		     // 设置响应的内容类型为JSON
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			response.getWriter().println(responseObject);
			//服务器端跳转
			//request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		 }
	}

}
